/**
 * 
 */
package com.projectName.www.service;

import com.projectName.www.po.coupons;
import com.projectName.www.util.String_util;
import com.projectName.www.util.find;
import com.projectName.www.dao.S_J_insertcoupons_dao;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.time.LocalDate;
import java.util.Vector;


/**
 * @author 86132
 *
 */
public class S_J_insertcoupons extends JInternalFrame {
	private JTextField coupon;
	private JTextField num;
	private JTextField year;
	private JTextField mouth;
	private JTextField day;
	private JTable table;
	coupons cou=new coupons();
	LocalDate finaltime;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					S_J_insertcoupons frame = new S_J_insertcoupons();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public S_J_insertcoupons() throws Exception {

		setTitle("发送优惠券");
		setMaximizable(true);
		setClosable(true);
		setBounds(100, 100, 756, 523);

		JDesktopPane desktopPane = new JDesktopPane();
		desktopPane.setBackground(SystemColor.inactiveCaptionBorder);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
								.addComponent(desktopPane, GroupLayout.PREFERRED_SIZE, 744, GroupLayout.PREFERRED_SIZE)
								.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
								.addComponent(desktopPane, GroupLayout.PREFERRED_SIZE, 491, GroupLayout.PREFERRED_SIZE)
								.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);

		JLabel lblNewLabel = new JLabel("优惠的金额：");

		coupon = new JTextField();
		coupon.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("优惠券的数量：");

		num = new JTextField();
		num.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("   截止时间：");

		year = new JTextField();
		year.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("年");

		mouth = new JTextField();
		mouth.setColumns(10);

		day = new JTextField();
		day.setColumns(10);

		JLabel lblNewLabel_3_1 = new JLabel("月");

		JLabel lblNewLabel_3_2 = new JLabel("日");

		JButton insertcou = new JButton("添加");
		insertcou.addActionListener(e -> {
			try {
				insert_cou(e);
			} catch (Exception ex) {
				throw new RuntimeException(ex);
			}
		});

		JButton closecou = new JButton("取消");
		closecou.addActionListener(e -> close_cou(e));

		JScrollPane scrollPane = new JScrollPane();
		GroupLayout gl_desktopPane = new GroupLayout(desktopPane);
		gl_desktopPane.setHorizontalGroup(
				gl_desktopPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_desktopPane.createSequentialGroup()
								.addGroup(gl_desktopPane.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_desktopPane.createSequentialGroup()
												.addGap(83)
												.addGroup(gl_desktopPane.createParallelGroup(Alignment.LEADING)
														.addComponent(lblNewLabel)
														.addComponent(lblNewLabel_1))
												.addGap(2)
												.addGroup(gl_desktopPane.createParallelGroup(Alignment.LEADING)
														.addComponent(coupon, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
														.addComponent(num, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
												.addGap(252)
												.addComponent(insertcou, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_desktopPane.createSequentialGroup()
												.addGap(71)
												.addGroup(gl_desktopPane.createParallelGroup(Alignment.LEADING)
														.addGroup(gl_desktopPane.createSequentialGroup()
																.addGap(80)
																.addComponent(year, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE))
														.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE))
												.addGap(8)
												.addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
												.addGap(10)
												.addComponent(mouth, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
												.addGap(10)
												.addComponent(lblNewLabel_3_1, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
												.addGap(3)
												.addComponent(day, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
												.addGap(13)
												.addComponent(lblNewLabel_3_2, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
												.addGap(120)
												.addComponent(closecou, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_desktopPane.createSequentialGroup()
												.addGap(33)
												.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 661, GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(50, Short.MAX_VALUE))
		);
		gl_desktopPane.setVerticalGroup(
				gl_desktopPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_desktopPane.createSequentialGroup()
								.addContainerGap()
								.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 188, GroupLayout.PREFERRED_SIZE)
								.addGap(87)
								.addGroup(gl_desktopPane.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_desktopPane.createSequentialGroup()
												.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
												.addGap(19)
												.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_desktopPane.createSequentialGroup()
												.addGap(8)
												.addComponent(coupon, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
												.addGap(23)
												.addComponent(num, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_desktopPane.createSequentialGroup()
												.addGap(3)
												.addComponent(insertcou, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)))
								.addGap(10)
								.addGroup(gl_desktopPane.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_desktopPane.createSequentialGroup()
												.addGap(4)
												.addComponent(year, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
										.addGroup(gl_desktopPane.createSequentialGroup()
												.addGap(4)
												.addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_desktopPane.createSequentialGroup()
												.addGap(4)
												.addComponent(mouth, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_desktopPane.createSequentialGroup()
												.addGap(4)
												.addComponent(lblNewLabel_3_1, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_desktopPane.createSequentialGroup()
												.addGap(4)
												.addComponent(day, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_desktopPane.createSequentialGroup()
												.addGap(7)
												.addComponent(lblNewLabel_3_2, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
										.addComponent(closecou, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)))
		);

		table = new JTable();
		table.setModel(new DefaultTableModel(
				new Object[][]{
						{null, null, null},
						{null, null, null},
						{null, null, null},
						{null, null, null},
						{null, null, null},
						{null, null, null},
						{null, null, null},
						{null, null, null},
						{null, null, null},
						{null, null, null},
				},
				new String[]{
						"\u4F18\u60E0\u91D1\u989D", "剩余数量", "\u622A\u81F3\u65F6\u95F4"
				}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(80);
		table.getColumnModel().getColumn(0).setMinWidth(16);
		scrollPane.setViewportView(table);
		desktopPane.setLayout(gl_desktopPane);
		getContentPane().setLayout(groupLayout);
		try {
			filltable();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private void close_cou(ActionEvent e) {
		coupon.setText("");
		num.setText("");
		year.setText("");
		mouth.setText("");
		day.setText("");
	}

	private void filltable() throws Exception {
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		model.setRowCount(0);//设置为0行
		ResultSet fill = S_J_insertcoupons_dao.fill();
		while (fill.next())
		{
			Vector v=new Vector();
			v.add(fill.getString("coupon"));
			v.add(fill.getString("num"));
			v.add(fill.getString("finaltime"));
			model.addRow(v);
		}
	}
	private void insert_cou(ActionEvent e) throws Exception {
		if(String_util.isEmpty((coupon.getText()))||String_util.isEmpty((num.getText()))||String_util.isEmpty(year.getText()
		)||String_util.isEmpty(mouth.getText())||String_util.isEmpty(day.getText()))
		{
			JOptionPane.showMessageDialog(null,"数量,金额,截止时间不能为空");
			return;
		}
		finaltime=LocalDate.of(Integer.parseInt(year.getText()),Integer.parseInt(mouth.getText()),Integer.parseInt(day.getText()));
		cou.setCoupon(Integer.parseInt(coupon.getText()));
		cou.setShopname(find.name(OpenBusiness.username));
		cou.setNum(Integer.parseInt(num.getText()));
		cou.setFinaltime(finaltime);
		boolean result=S_J_insertcoupons_dao.insertcou(cou);
		if(result)
		{
			JOptionPane.showMessageDialog(null,"发放成功");
			filltable();
		}
		else JOptionPane.showMessageDialog(null,"发放失败");
	}
}
